package com.cloud.school.bus.teacherhelper.modules.home.camera;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.uploadpic.UploadPic;
import com.cloud.school.bus.teacherhelper.modules.home.CameraBigPictureActivity;
import com.cloud.school.bus.teacherhelper.modules.home.recorder.FFmpegRecorderActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.SurfaceHolder.Callback;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 自定义照相机
 * 
 * @author lenovo
 * 
 */
public class CustomCameraActivity extends Activity {

	private SurfaceView mSurfaceview;
	protected Camera camera;
	protected boolean isPreview;
	private boolean isFlash;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_custom_camera);

		mSurfaceview = (SurfaceView) this.findViewById(R.id.arc_hf_video_view);

		// 绑定预览视图
		SurfaceHolder holder = mSurfaceview.getHolder();
		holder.addCallback(new Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				if (camera != null) {
					if (isPreview) {
						camera.stopPreview();
						isPreview = false;
					}
					camera.release();
					camera = null; // 记得释放
				}
				mSurfaceview = null;
			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				try {
					camera = Camera.open();
					Camera.Parameters parameters = camera.getParameters();
					List<String> focusModes = parameters.getSupportedFocusModes();
					if (focusModes
							.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
						parameters
								.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
					} else if (focusModes
							.contains(Camera.Parameters.FOCUS_MODE_FIXED)) {

						parameters
								.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
					} else if (focusModes
							.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
						parameters
								.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
					}
					parameters.setPreviewFrameRate(30); // 每秒5帧
					parameters.setPictureFormat(PixelFormat.JPEG);// 设置照片的输出格式
					parameters.set("jpeg-quality", 85);// 照片质量
					parameters.setRotation(90);
					// 拍照时，开启闪光灯
					parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
					camera.setParameters(parameters);
					camera.setPreviewDisplay(holder);
					camera.setDisplayOrientation(90);
					camera.startPreview();
					isPreview = true;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {
			}
		});
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		Button startCameraButton = (Button) findViewById(R.id.startCameraButton);
		startCameraButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (camera != null) {

					camera.takePicture(null, null, new PictureCallback() {
						@Override
						public void onPictureTaken(byte[] data, Camera camera) {

							String path = UploadPic.SDCARD_ROOT_PATH + "/"
									+ UploadPic.DCIM + "/"
									+ UploadPic.APP_PIC_FOLDER + "/";
							String fileName = new SimpleDateFormat(
									"yyyyMMddHHmmss").format(new Date())
									+ ".jpg";
							File out = new File(path);
							if (!out.exists()) {
								out.mkdirs();
							}
							out = new File(path, fileName);
							try {
								FileOutputStream outStream = new FileOutputStream(
										out);
								outStream.write(data, 0, data.length);
								outStream.flush();
								outStream.close();
							} catch (Exception e) {
								e.printStackTrace();
							}
							// showMsg("拍照成功");
							if (camera != null) {
								if (isPreview) {
									camera.stopPreview();
									isPreview = false;
								}
								camera.release();
								camera = null; // 记得释放
							}
							mSurfaceview = null;
							
							String imagePathString =  path+fileName;
							
//							int degree = ImageUtil.readPictureDegree(imagePathString);
//							Bitmap bitmap = ThumbnailUtils.setThumbnailBitmap(new File(
//									imagePathString), 480, 800);
//							
//							if (Math.abs(degree) > 0) {
//								bitmap = ImageUtil.rotaingImageView(degree, bitmap);
//								
//							}else {
//								
//							}
//							File file = new File(imagePathString);
//							if(file.exists()){
//								file.delete();
//							}
//							ImageUtil.saveBitmap(bitmap, imagePathString, CompressFormat.JPEG);
							
							Intent intent = new Intent(CustomCameraActivity.this,
									CameraBigPictureActivity.class);
							intent.putExtra("picPath", imagePathString);
							startActivityForResult(intent, 0);
							finish();
						}
					}); // 拍照
				}
			}
		});
		Button switchButton = (Button) findViewById(R.id.switchButton);
		switchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (camera != null) {
					if (isPreview) {
						camera.stopPreview();
						isPreview = false;
					}
					camera.release();
					camera = null; // 记得释放
				}
				mSurfaceview = null;
				Intent intent = new Intent(CustomCameraActivity.this,
						FFmpegRecorderActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				finish();
			}
		});
		Button cameraCancelButton = (Button) findViewById(R.id.cameraCancelButton);
		cameraCancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		final Button flashButton = (Button)findViewById(R.id.flashButton);
		flashButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Camera.Parameters parameters = camera.getParameters();
				if(isFlash){
					isFlash = false;
//					flashButton.setText("禁止闪光");
					flashButton.setBackgroundResource(R.drawable.btn_camera_glyph_flash_off);
					parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
				}else {
					isFlash = true;
//					flashButton.setText("强制闪光");
					flashButton.setBackgroundResource(R.drawable.btn_camera_glyph_flash_on);
					parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
				}
				camera.setParameters(parameters);
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (camera != null) {
			if (isPreview) {
				camera.stopPreview();
				isPreview = false;
			}
			camera.release();
			camera = null; // 记得释放
		}
		mSurfaceview = null;
	}

	// /*
	// * 消息提示
	// */
	// private Toast toast;
	//
	// public void showMsg(String arg) {
	// if (toast == null) {
	// toast = Toast.makeText(this, arg, Toast.LENGTH_SHORT);
	// } else {
	// toast.cancel();
	// toast.setText(arg);
	// }
	// toast.show();
	// }

	/*
	 * 格式化时间
	 */
	public String format(int i) {
		String s = i + "";
		if (s.length() == 1) {
			s = "0" + s;
		}
		return s;
	}

}
